<?php
namespace app\modules\backend\controllers;

use app\models\Area;
use app\models\Shop;
use app\models\Base;
use app\tools\Re;
use Yii;

class AreaController extends BaseController
{
    //城市接口(二级)
    public function actionCityList(){
        $city_data = [];
        $citys_one = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($citys_one)) {
            foreach ($citys_one as $v1) {
                $city_two_data = [];
                $citys_two = Area::find()->where(['parent_id' => $v1->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($citys_two)) {
                    foreach ($citys_two as $v2) {
                        $city_two_data[] = [
                            'value' => (int)$v2->id,
                            'label' => $v2->short_name,
                        ];
                    }
                }
                $city_data[] = [
                    'value' => (int)$v1->id,
                    'label' => $v1->short_name,
                    'children' => $city_two_data
                ];
            }
        }
        return Re::data(['data' => $city_data]);
    }

    //城市接口(三级)
    public function actionThreeCityList(){
        $type = Yii::$app->request->post('type', 0);
        $city_data = [];
        $citys_one = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($citys_one)) {
            foreach ($citys_one as $v1) {
                $city_two_data = [];
                $citys_two = Area::find()->where(['parent_id' => $v1->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($citys_two)) {
                    foreach ($citys_two as $v2) {
                        $city_three_data = [];
                        $citys_three = Area::find()->where(['parent_id' => $v2->id, 'level' => 2])->andWhere("short_name <> '$v2->short_name'")->orderBy("area_code asc")->all();
                        if (!empty($citys_three)) {
                            foreach ($citys_three as $v3) {
                                $city_three_data[] = [
                                    'value' => (int)$v3->id,
                                    'label' => $v3->short_name,
                                ];
                            }
                        }
                        $city_two_data[] = [
                            'value' => (int)$v2->id,
                            'label' => $v2->short_name,
                            'children' => $city_three_data
                        ];
                    }
                }
                $city_data[] = [
                    'value' => (int)$v1->id,
                    'label' => $v1->short_name,
                    'children' => $city_two_data
                ];
            }
        }
        return Re::data(['data' => $city_data]);
    }

    //城市接口(按照门店区域来读取)
    public function actionThreeCityListForShop(){
        $city_data = [];
        $province_id_arr = Shop::find()->where(['status' => 1])->select("province_id")->column();
        $city_id_arr = Shop::find()->where(['status' => 1])->select("city_id")->column();
        $area_id_arr = Shop::find()->where(['status' => 1])->select("area_id")->column();
        if ($province_id_arr) {
            $citys_one = Area::find()->where(['parent_id' => 0, 'id' => $province_id_arr])->orderBy("area_code asc")->all();
            if (!empty($citys_one)) {
                foreach ($citys_one as $v1) {
                    $city_two_data = [];
                    $citys_two = Area::find()->where(['parent_id' => $v1->id, 'level' => 1, 'id' => $city_id_arr])->orderBy("area_code asc")->all();
                    if (!empty($citys_two)) {
                        foreach ($citys_two as $v2) {
                            $city_three_data = [];
                            $citys_three = Area::find()->where(['parent_id' => $v2->id, 'level' => 2, 'id' => $area_id_arr])->andWhere("short_name <> '$v2->short_name'")->orderBy("area_code asc")->all();
                            if (!empty($citys_three)) {
                                foreach ($citys_three as $v3) {
                                    $city_three_data[] = [
                                        'value' => (int)$v3->id,
                                        'label' => $v3->name,
                                    ];
                                }
                            }
                            $city_two_data[] = [
                                'value' => (int)$v2->id,
                                'label' => $v2->name,
                                'children' => $city_three_data
                            ];
                        }
                    }
                    $city_data[] = [
                        'value' => (int)$v1->id,
                        'label' => $v1->name,
                        'children' => $city_two_data
                    ];
                }
            }
        }
        return Re::data(['data' => $city_data]);
    }

    //城市接口
    public function actionAllAreaList(){
        $area_id = Yii::$app->request->post('area_id', '');
        $area_arr = $area_id ? explode(',', $area_id) : [];
        $all_area_data = [];
        $province_list = Area::find()->where(['parent_id' => 0])->orderBy("area_code asc")->all();
        if (!empty($province_list)) {
            foreach ($province_list as $p) {
                $city_data = [];
                $city_list = Area::find()->where(['parent_id' => $p->id, 'level' => 1])->orderBy("area_code asc")->all();
                if (!empty($city_list)) {
                    foreach ($city_list as $c) {
                        $area_data = [];
                        $area_list = Area::find()->where(['parent_id' => $c->id, 'level' => 2])->andWhere("short_name <> '$c->short_name'")->orderBy("area_code asc")->all();
                        if (!empty($area_list)) {
                            foreach ($area_list as $a) {
                                $area_data[] = [
                                    'id' => $a->id,
                                    'title' => $a->name,
                                    'checked' => in_array($a->id, $area_arr)
                                ];
                            }
                        }
                        $city_data[] = [
                            'id' => $a->id,
                            'title' => $c->name,
                            'children' => $area_data
                        ];
                    }
                }
                $all_area_data[] = [
                    'id' => $a->id,
                    'title' => $p->name,
                    'children' => $city_data
                ];
            }
        }
        return Re::data(['data' => $all_area_data]);
    }

    //城市接口(二级)
    public function actionGetAreaInfo(){
        $id = Yii::$app->request->post('id', 0);
        $area_info = Area::findOne($id);
        if (empty($id) || empty($area_info)) {
            return Re::data(null, '信息读取失败', Re::ERROR);
        }
        return Re::data(['data' => $area_info]);
    }
}
